package com.hejjon.controller;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * Created by caoshi at 20:59 2021-08-15
 */
@RestController
public class TestQuartzController {


//    @RequestMapping("test")
//    public String test() throws SchedulerException, InterruptedException {
//
//    }


    public static void main(String[] args) throws SchedulerException, InterruptedException {

        // 1. 创建调度器scheduler
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        // 2. 创建 jobDetail 实例, 并绑定执行类
        int detailCount = 0;
        JobDetail detail = JobBuilder.newJob(HelloQuartz.class)
                .withIdentity("Quartz1", "QuartzGroup1")
                .usingJobData("detailKey", "detail")
                .usingJobData("name", "zhangsan")
                .usingJobData("detailCount", detailCount)
                .build();

        int triggerCount = 0;
        // 3. 创建触发器 trigger 实例
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("Trigger1", "QuartzTrigger1")
                .usingJobData("trigger", "triggerValue")
                .usingJobData("triggerCount", triggerCount)
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
                .build();

        // 4. 执行quartz
        scheduler.scheduleJob(detail, trigger);
        System.out.println("--------scheduler start ! ------------");
        scheduler.start();
//        scheduler.shutdown();
        System.out.println("--------scheduler shutdown ! ------------");
    }

}
